package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.ProvinceBean;
import com.zhuoshi.utils.DbHelper;

public class ProvinceDao {
	public int add(ProvinceBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("insert into province (areaid,province) values(?,?)");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, bean.getAreaid());
			pstat.setString(2, bean.getProvince());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}
	
	
	public List<ProvinceBean> getList(){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<ProvinceBean> list = new ArrayList<ProvinceBean>();
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from province where 1=1");
			//sql.append(" and id=? ");
			pstat = conn.prepareStatement(sql.toString());
			//pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while(rs.next()){
				ProvinceBean bean = new ProvinceBean();
				
				bean.setId(rs.getInt("id"));
				bean.setAreaid(rs.getInt("areaid"));
				bean.setProvince(rs.getString("province"));
				list.add(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}
	
	public List<ProvinceBean> getListByAreaid(int areaid){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<ProvinceBean> list = new ArrayList<ProvinceBean>();
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from province where areaid=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, areaid);
			rs = pstat.executeQuery();
			while(rs.next()){
				ProvinceBean bean = new ProvinceBean();
				
				bean.setId(rs.getInt("id"));
				bean.setAreaid(rs.getInt("areaid"));
				bean.setProvince(rs.getString("province"));
				list.add(bean);
				//System.out.println(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}
	
	
	public int update(ProvinceBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("update province set areaid=?,province=? where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, bean.getAreaid());
			pstat.setString(2, bean.getProvince());
			pstat.setInt(3, bean.getId());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}
	
	public int delete(int id){
		
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("delete from province where id=?");
			pstat = conn.prepareStatement(sql.toString());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}
	
	public ProvinceBean getModel(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		ProvinceBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from province where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while(rs.next()){
				bean = new ProvinceBean();
				bean.setId(rs.getInt("id"));
				bean.setAreaid(rs.getInt("areaid"));
				bean.setProvince(rs.getString("province"));
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return bean;
	}
	
	
}
